package com.example.wc.logindemo.getdownload;

import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.util.LruCache;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.example.wc.logindemo.R;
import com.example.wc.logindemo.VolleyRequestDemo;

import java.util.List;

public class ImageLoaderActivity extends AppCompatActivity {

    private Button mDownloadButton;
    private ImageView mDownloadImageView;
    private RequestQueue mQueue;
    private VolleyRequestDemo mVolleyRequestDemo;
    private ImageLab.Images mImages;
    private List<Image> mImageList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_loader);

        mDownloadButton = (Button) findViewById(R.id.download_button_image_loader_activity);
        mDownloadButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(mVolleyRequestDemo == null){
                    mVolleyRequestDemo = new VolleyRequestDemo();
                }
                mVolleyRequestDemo.volleyRequestDemo_GET_Image(ImageLoaderActivity.this,
                        new VolleyRequestDemo.ImageGetListener() {
                    @Override
                    public void onImageGeted(ImageLab.Images images) {
                        //这里是子线程，子线程还未结束，调用了更新界面
                        mImages = images;
                        Log.d("GET_TAG", mImages.toString());
                        mImageList = mImages.data;

                    }
                });

                mQueue = Volley.newRequestQueue(ImageLoaderActivity.this);
                ImageLoader imageLoader = new ImageLoader(mQueue, new BitmapCache());

                ImageLoader.ImageListener listener = ImageLoader.getImageListener(mDownloadImageView,
                        R.drawable.ic_blogs_add, R.mipmap.ic_launcher);

                imageLoader.get(mImageList.get(1).getUrl(), listener, 200 ,200);

            }
        });
        mDownloadImageView = (ImageView) findViewById(R.id.download_image_view_image_loader_activity);
    }

//    public class BitmapCache implements ImageLoader.ImageCache {
//
//        private LruCache<String, Bitmap> mCache;
//
//        public BitmapCache() {
//            int maxSize = 10 * 1024 * 1024;
//            mCache = new LruCache<String, Bitmap>(maxSize) {
//                @Override
//                protected int sizeOf(String key, Bitmap bitmap) {
//                    return bitmap.getRowBytes() * bitmap.getHeight();
//                }
//            };
//        }
//
//        @Override
//        public Bitmap getBitmap(String url) {
//            return mCache.get(url);
//        }
//
//        @Override
//        public void putBitmap(String url, Bitmap bitmap) {
//            mCache.put(url, bitmap);
//        }
//
//    }
}
